package com.videoaggre.zcl.ui.widget

import android.annotation.SuppressLint
import android.content.Context
import android.content.res.TypedArray
import android.os.Handler
import android.os.Message
import android.util.AttributeSet
import android.view.Gravity
import android.view.View
import androidx.appcompat.widget.AppCompatTextView
import com.videoaggre.zcl.R
import com.videoaggre.zcl.common.toColor
import java.util.*

class TimeButton : AppCompatTextView, View.OnClickListener {
    companion object {
        private const val showText = "秒后重发"
        private const val NorText = "获取验证码"
    }

    private var mTimer: Timer? = null
    private var mTimerTask: TimerTask? = null
    private var mTime: Long = 0
    private var mLenght: Long = 60000
    private var isStart = false
    private var normalColor = R.color.text_color.toColor()
    private var hintColor = R.color.text_hint_color.toColor()
    private var defaultEnable = true
    protected var mOnClickListener: OnClickListener? = null

    constructor(context: Context) : this(context, null)
    constructor(context: Context, attrs: AttributeSet?) : super(context, attrs) {
        init(attrs)
    }

    fun init(attrs: AttributeSet?) {
        attrs?.let {
            val typedArray: TypedArray = context.obtainStyledAttributes(attrs, R.styleable.TimeButton)
            normalColor = typedArray.getColor(R.styleable.TimeButton_time_button_normal, normalColor)
            hintColor = typedArray.getColor(R.styleable.TimeButton_time_button_enable_false, hintColor)
            defaultEnable = typedArray.getBoolean(R.styleable.TimeButton_defaultEnable, true)
            typedArray.recycle()
        }
        setTextColor(if (defaultEnable) normalColor else hintColor)
        textSize = 12f
        text = NorText
        gravity = Gravity.CENTER
        setOnClickListener(this)
    }

    override fun onClick(v: View?) {
        if (mOnClickListener != null && !isStart && defaultEnable) {
            mOnClickListener!!.onClick(v)
        }
    }

    override fun setOnClickListener(l: OnClickListener?) {
        if (this == l)
            super.setOnClickListener(this)
        else
            mOnClickListener = l
    }

    @SuppressLint("HandlerLeak")
    var mHandler: Handler = object : Handler() {
        override fun handleMessage(msg: Message) {
            if (mTime <= 0) {
                isStart = false
                setTextColor(normalColor)
                text = NorText
            } else {
                setText(getShowText())
            }
        }
    }

    private fun initTimer() {
        mTime = mLenght
        mTimer = Timer()
        mTimerTask = object : TimerTask() {
            override fun run() {
                mTime -= 1000 //倒计时
                mHandler.sendEmptyMessage(0)
                if (mTime <= 0) {
                    clearTimer()
                }
            }
        }
    }

    private fun clearTimer() {
        mTimerTask?.cancel()
        mTimerTask = null
        mTimer?.cancel()
        mTimer = null
    }

    fun enable(enable: Boolean): Boolean {
        if (enable && !defaultEnable) {
            setTextColor(normalColor)
        }
        if (!enable && defaultEnable) {
            setTextColor(hintColor)
        }
        defaultEnable = enable
        return enable
    }

    fun enable() = defaultEnable

    fun start() {
        setTextColor(hintColor)
        initTimer()
        isStart = true
        mTimer!!.schedule(mTimerTask, 0, 1000)
    }

    fun setTimeLenght(lenght: Long) {
        mLenght = lenght
    }

    private fun getShowText(): String {
        return if (mTime <= 0) NorText else (mTime / 1000).toString() + showText
    }

    override fun onDetachedFromWindow() {
        super.onDetachedFromWindow()
        mHandler.removeCallbacks({})
    }
}